package com.molichuxing.services.infrastructure.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.AdvertisingStoreBean;
import com.molichuxing.services.infrastructure.dao.AdvertisingStoreMapper;
import com.molichuxing.services.infrastructure.service.AdvertisingStoreService;

@Service("advertisingStoreService")
public class AdvertisingStoreServiceImpl implements AdvertisingStoreService{

	 @Resource
	 private AdvertisingStoreMapper advertisingStoreMapper;
	 
	 /**
	  * 根据门店id查询广告id
	  * @param storeId
	  * @return
	  */	
	 @Override
	 public Integer getAdvertIdByStoreId(Integer storeId) {
		Integer advertId=advertisingStoreMapper.getAdvertIdByStoreId(storeId);
		return advertId;
	}
	 
	 /**
	  * 根据广告id查询绑定门店数量
	  * @param advertId
	  * @return
	  */ 	
	@Override
	public Integer getStoreCountByAdvertId(Integer advertId) {
		Integer count=advertisingStoreMapper.getStoreCountByAdvertId(advertId);
		return count;
	}
	
	/**
	 * 根据广告id获取门店id
	 * @param advertId
	 * @return
	 */
	@Override
	public List<Integer> getAdvertisingStoreByAdvertId(Integer advertId) {
		List<AdvertisingStoreBean> listBean=advertisingStoreMapper.getAdvertisingStoreByAdvertId(advertId);
		if(null == listBean || listBean.isEmpty()) {
			return null;
		}
		List<Integer> list = new ArrayList<>(listBean.size());
		for (AdvertisingStoreBean bean : listBean) {
			list.add(bean.getStoreId());
		}
		return list;
	}
	
	/**
	 * 新增门店与广告的关系
	 * @param storeIds
	 * @param advertId
	 * @return
	 */
	@Override
	public Boolean createAdvertStore(List<Integer> storeIds, Integer advertId) {
			advertisingStoreMapper.deleteByAdvertId(advertId);
			if(null != storeIds && !storeIds.isEmpty()) {
			  for (Integer storeId : storeIds) {
				//先删除之前绑定关系
				advertisingStoreMapper.deleteByStoreId(storeId);
				//重新添加新的绑定关系
				create(storeId,advertId);
			  }
		   }
		return true;
	}
	
	/**
	 * 新增数据
	 * @param storeId
	 * @param advertId
	 * @return
	 */
	private Boolean create(Integer storeId,Integer advertId) {
		AdvertisingStoreBean t = new AdvertisingStoreBean();
		t.setStoreId(storeId);
		t.setAdvertId(advertId);
		int num = advertisingStoreMapper.create(t);
		return num==1;
	}
}
